# -*- coding: utf8 -*-

from puppy.model import Query

class Service(object):
  """
  A PuppyIR Search Service.
  
  Establishes the configuration of the QueryFilter pipeline, QuerySuggestion pipeline, Search Engine, and the ResultFilter pipeline.
  
  TODO: consider enable / disable methods for service config
        
        consider removal (all or specific)
  """
  
  def __init__(self):
    """Constructor for Service."""
    super(Service, self).__init__()
    self.query_filters = []
    self.search_suggestions = []
    self.search_engine = ""
    self.result_filters = []
    self.queryID = 0
    self.resultID = 0
  
  def search(self, query):
    """
    Run a search on the SearchService.
    
    Parameters:
    
    * query (:class:`Query`): user query
    
    Returns:
    
    * results (puppy.model.Opensearch.Results): search results
    """    
    
    query = Query(query)

    # run query filters
    for query_filter in self.query_filters:
      query = query_filter.run(query)

    # run search suggestions
    for search_suggestion in self.search_suggestions:
      query = search_suggestion.run(query)
    
    # submit query to search engine
    results = self.search_engine.search(query)
    
    # run result filters
    # for result_filter in self.result_filters:
    #   results = result_filter.run(results)

    return results
  
  
  def add_query_filter(self, query_filter):
    """Add filter to query filter pipeline."""
    self.query_filters.append(query_filter)
  
  
  def add_result_filter(self, result_filter):
    """Add filter to result filter pipeline."""
    self.result_filters.append(result_filter)
  
  
  def add_search_suggestion(self, search_suggestion_filter):
    """Add filter to search suggestion filter pipeline."""
    self.search_suggestions.append(search_suggestion_filter)  
  
  
  def set_search_engine(self, search_engine):
    """Add search engine to service."""
    self.search_engine = search_engine
  

